package net.andersand.games.yatzy;

import java.util.LinkedHashSet;
import java.util.Set;

class ScoreKeeper {
	private Set<Score> scores;
	private int playerID;
	
	ScoreKeeper(int playerID) {
		scores = new LinkedHashSet<Score>();
		this.playerID = playerID;
	}
	
	/**
	 * Adds the score (if that type was not already set).
	 * 
	 * @param score The score that is to be registered
	 * @return true if successfuly added the score
	 */
	boolean addScore(Score score) {
		score.setPlayerID(playerID);
		for (Score s : scores) {
			if (s.getType().equals(score.getType())) {
				return false;
			}
		}
		scores.add(score);
		return true;
	}
	
	Set<Score> getScore() {
		return scores;
	}

	Score findScoreByOrdinal(int rowIndex) {
		for (Score s : scores) {
			if (s.getType().ordinal() == rowIndex) {
				return s;
			}
		}
		return null;
	}
	
	int sum() {
		int sum = 0;
		for (Score s : scores) {
			sum += s.getValue();
		}
		return sum;
	}
}
